# **************************************************************************
# Makefile
#
# Makefile for creating MS Windows installers for the SoQt binary SDK for
# MS Windows.
#
# Prerequisites:
#   Cygwin system.
#   makensis - the nullsoft installer system
#
# Authors:
#   Lars J. Aas <larsa@sim.no>
# **************************************************************************

project = soqt
Project = SoQt

major = @SOQT_MAJOR_VERSION@

sdk_prefix = /tmp/coin3d/$(project)
top_srcdir = @soqt_src_dir@

# default c library option
libc = md

ALLCFLAGS = # /WX /noBool
# Qt uses bool, so /noBool can't be used
DBGCFLAGS = $(ALLCFLAGS) /GZ /Zi /Yd
OPTCFLAGS = $(ALLCFLAGS) /Oitgb1 /Gs
DLLCFLAGS = /GD

CONFIGUREOPTIONS =
BUILDOPTIONS = --disable-dependency-tracking

BUILDS = buildfiles docs shared-release shared-debug # static-release static-debug

# **************************************************************************

all:: $(BUILDS) $(project)-v$(VERSION)-install.exe

# **************************************************************************

extract.sed: Makefile
	@echo -e "2,\$$ {\n/GENERAL PUBLIC LICENSE/,/END OF TERMS/ {\ns/\f//;\np;\n};\n};" >extract.sed

unix2dos.sed: Makefile
	@echo -e "s/\$$/\r/;\np;" >unix2dos.sed

LICENSE.TXT: extract.sed unix2dos.sed
	@cp $(top_srcdir)/COPYING LICENSE.TXT;
	@for process in extract unix2dos; do \
	  sed -n -f $${process}.sed <LICENSE.TXT >LICENSE.TMP; \
	  rm -f LICENSE.TXT; \
	  mv -f LICENSE.TMP LICENSE.TXT; \
	  rm -f $${process}.sed; \
	done

# FIXME: use full makensis path (and support spaces in the path)...
# something like ac_subst $(sim_ac_makensis_exe)
$(project)-v$(VERSION)-install.exe: $(project).nsi LICENSE.TXT $(project)-sdk.html
	makensis $(project).nsi

$(Project)-$(VERSION): $(Project)-$(VERSION).tar.gz
	tar zxf $(Project)-$(VERSION).tar.gz

$(Project)-$(VERSION).tar.gz:
	@( cd ..; $(MAKE) dist; mv $(Project)-$(VERSION).tar.gz windows/ )

headerlist.nsi: Makefile
	winpath=`CYGWIN= cygpath -w "$(sdk_prefix)"`; \
	( prevdir=; cd $(sdk_prefix); for path in `find include -type f`; do \
	  dir=`echo $$path | sed 's/\/[^\/]*$$//'`; \
	  if test x"$$dir" != x"$$prevdir"; then \
	    prevdir=$$dir; \
	    file=`echo $$path | sed 's/^.*\///g'`; \
	    dir=`echo $$dir | sed 's/\//\\\\/g'`; \
	    echo "SetOutPath \$$INSTDIR\\$$dir"; \
	    echo "File $$winpath\\$$dir\\*.h"; \
	  fi; \
	done ) >headerlist.nsi

sourcelist.nsi: $(Project)-$(VERSION) Makefile
	( prevdir=""; for path in `find $(Project)-$(VERSION) -type f`; do \
	  dir=`echo $$path | sed 's/\/[^\/]*$$//'`; \
	  if test x"$$dir" != x"$$prevdir"; then \
	    prevdir=$$dir; \
	    file=`echo $$path | sed 's/^.*\///g'`; \
	    dir=`echo $$dir | sed 's/\//\\\\/g'`; \
	    echo "SetOutPath \$$INSTDIR\\source\\$$dir"; \
	    echo "File $$dir\\*"; \
	  fi; \
	done ) >sourcelist.nsi

buildfileslist.nsi: buildfiles Makefile
	( prevdir=; cd build-files; for path in ./$(project)$(major).* `find . -name "*.h" -o -name "*.cpp" -o -name "*.ih" -o -name "*.icc"`; do \
	  dir=`echo $$path | sed 's/\/[^\/]*$$//'`; \
	  if test x"$$dir" != x"$$prevdir"; then \
	    prevdir=$$dir; \
	    file=`echo $$path | sed 's/^.*\///g'`; \
	    dir=`echo $$dir | sed 's/\//\\\\/g' | cut -c2-`; \
	    echo "SetOutPath \$$INSTDIR\\build$$dir"; \
	    echo "File .\\build-files$$dir\\$$file"; \
	  else \
	    dir=`echo $$dir | sed 's/\//\\\\/g' | cut -c2-`; \
	    file=`echo $$path | sed 's/^.*\///g'`; \
	    echo "File .\\build-files$$dir\\$$file"; \
	  fi; \
	done ) >buildfileslist.nsi

uninstall.nsi: Makefile
	( cd $(sdk_prefix); \
	for file in `find . -type f -depth`; do \
	  file=`echo $$file | sed 's/^.\///; s/\//\\\\/g'`; \
	  echo "Delete \$$INSTDIR\\$$file"; \
	done; \
	for dir in `find . -type d -depth`; do \
	  dir=`echo $$dir | sed 's/^.\///; s/\//\\\\/g'`; \
	  echo "RMDir \$$INSTDIR\\$$dir"; \
	done ) >uninstall.nsi

$(project).nsi: heading.nsi headerlist.nsi middle.nsi sourcelist.nsi bfheader.nsi buildfileslist.nsi post.nsi uninstall.nsi footer.nsi unix2dos.sed Makefile
	cat heading.nsi headerlist.nsi middle.nsi sourcelist.nsi bfheader.nsi buildfileslist.nsi post.nsi uninstall.nsi footer.nsi >$(project).tmp
	winpath=`CYGWIN= cygpath -w "$(sdk_prefix)"`; \
	winpath=`echo $$winpath | sed -e 's/\\\\/\\\\\\\\/g'`; \
	sed -e 's/@win_prefix@/'$$winpath'/g' \
	    -e 's/@sdk_version@/$(VERSION)/g' <$(project).tmp >$(project).tmp2
	sed -n -f unix2dos.sed <$(project).tmp2 >$(project).nsi
	rm -f $(project).tmp $(project).tmp2

# **************************************************************************

build-docs/html/index.html: $(Project)-$(VERSION)
	@if test -d build-docs; then :; else mkdir build-docs; fi
	cd build-docs; \
	../$(Project)-$(VERSION)/configure \
	  --disable-build \
	  --enable-html htmldir=html/$(Project) \
	  --enable-html-help htmlhelpdir=html \
	  --enable-man \
	  --prefix=$(sdk_prefix)
	cd build-docs/; \
	$(MAKE)
	cd build-docs/; \
	$(MAKE) install

docs: build-docs/html/index.html

build-shared-release/src/Inventor/Qt/$(project)$(major).dll: $(Project)-$(VERSION)
	@if test -d build-shared-release; then :; else mkdir build-shared-release; fi
	cd build-shared-release; \
	../$(Project)-$(VERSION)/configure \
	  CFLAGS="$(OPTCFLAGS) $(DLLCFLAGS)" \
	  CXXFLAGS="$(OPTCFLAGS) $(DLLCFLAGS)" \
	  $(CONFIGUREOPTIONS) \
	  $(BUILDOPTIONS) \
	  --disable-debug \
	  --disable-symbols \
	  --with-msvcrt=$(libc) \
	  --prefix=$(sdk_prefix)
	cd build-shared-release; \
	$(MAKE)
	cd build-shared-release; \
	$(MAKE) install

shared-release: build-shared-release/src/Inventor/Qt/$(project)$(major).dll

build-shared-debug/src/Inventor/Qt/$(project)$(major)d.dll: $(Project)-$(VERSION)
	@if test -d build-shared-debug; then :; else mkdir build-shared-debug; fi
	cd build-shared-debug; \
	../$(Project)-$(VERSION)/configure \
	  CFLAGS="$(DBGCFLAGS)" \
	  CXXFLAGS="$(DBGCFLAGS)" \
	  $(CONFIGUREOPTIONS) \
	  $(BUILDOPTIONS) \
	  --enable-debug \
	  --enable-symbols \
	  --with-msvcrt=$(libc)d \
	  --with-alternate=debug \
	  --with-suffix=d \
	  --prefix=$(sdk_prefix)
	cd build-shared-debug; \
	$(MAKE)
	cd build-shared-debug; \
	$(MAKE) install

shared-debug: build-shared-debug/src/Inventor/Qt/$(project)$(major)d.dll

build-static-release/src/Inventor/Qt/$(project)$(major)d.dll: $(Project)-$(VERSION)
	@if test -d build-static-release; then :; else mkdir build-static-release; fi
	cd build-static-release; \
	../$(Project)-$(VERSION)/configure \
	  CFLAGS="$(OPTCFLAGS)" \
	  CXXFLAGS="$(OPTCFLAGS)" \
	  $(CONFIGUREOPTIONS) \
	  $(BUILDOPTIONS) \
	  --disable-shared \
	  --disable-debug \
	  --disable-symbols \
	  --with-msvcrt=$(libc) \
	  --with-alternate=static \
	  --with-suffix=s \
	  --prefix=$(sdk_prefix)
	cd build-static-release; \
	$(MAKE)
	cd build-static-release; \
	$(MAKE) install

static-release: build-static-release/src/Inventor/Qt/$(project)$(major)d.dll

build-static-debug/src/Inventor/Qt/$(project)$(major)d.dll: $(Project)-$(VERSION)
	@if test -d build-static-debug; then :; else mkdir build-static-debug; fi
	cd build-static-debug; \
	../$(Project)-$(VERSION)/configure \
	  CFLAGS="$(DBGCFLAGS)" \
	  CXXFLAGS="$(DBGCFLAGS)" \
	  $(CONFIGUREOPTIONS) \
	  $(BUILDOPTIONS) \
	  --disable-shared \
	  --enable-debug \
	  --enable-symbols \
	  --with-msvcrt=$(libc)d \
	  --with-alternate=static-debug \
	  --with-suffix=sd \
	  --prefix=$(sdk_prefix)
	cd build-static-debug; \
	$(MAKE)
	cd build-static-debug; \
	$(MAKE) install

static-debug: build-static-debug/src/Inventor/Qt/$(project)$(major)d.dll

buildfiles: build-files/$(project)$(major).dsp

build-files/$(project)$(major).dsp: $(Project)-$(VERSION)
	@if test -d build-files; then :; else mkdir build-files; fi
	cd build-files; \
	../$(Project)-$(VERSION)/configure \
	  $(CONFIGUREOPTIONS) \
	  $(BUILDOPTIONS) \
	  --enable-msvcdsp; \
	$(MAKE) clean && $(MAKE)

.PHONY: docs shared-release shared-debug static-release static-debug buildfiles

# **************************************************************************
